import time
from py2neo import Node, Relationship,Graph


graph = Graph('http://localhost:7474', username='neo4j', password='123456')
def add_names(items, tx):
    for data in items:
        tx.append(statement_c, data)
    tx.process()

def main():
    with open("./raw.csv", "r") as f:
        content = f.readlines()

    items = []
    for index, c in enumerate(content):
        print(">>> {}".format(index))
        c = c.strip()

        person_name, company_name, visit_time = c.split(",")

        data = {
            "person_name": person_name,
            "company_name": company_name,
            "visit_time": visit_time,
        }
        items.append(data)

        if index % 1000 == 0:
            tx = graph.begin()
            add_names(items, tx)
            items = []
            tx.commit()


if __name__ == '__main__':
    s = time.time()
    statement_c = """MERGE (node1: m{person_name:{person_name}})
                     MERGE (node2:Company {company_name:{company_name}})
                     MERGE (node1)<-[:Query {visit_time: {visit_time}}]-(node2)"""
    main()
    e = time.time()
    print("耗时：{}s".format(e-s))